package com.web.utils;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

/**
 * Http请求获取开奖数据
 *
 * @author yangzhongying mail:223450427@qq.com
 * @date 2016-11-10 下午02:29:40
 * @package com.web.utils
 */
public class HttpRequest {

    //token	用户账号	必填，唯一标识。例：token=a1b2c3d4f5
    //code	彩票代码	部份接口格式支持逗号分割的多种彩票。例：code=cqssc,cqklsf
    //rows	返回行数	仅按最新查询时有效，默认5行。例：rows=3
    //format	数据格式	支持xml与json格式，默认xml。例：format=json
    //date	日期参数	仅按日期查询时有效，格式yyyy-MM-dd，低频返回当年。例：date=2015-02-18

    // 开发
    //public static String pkUrl = "http://118.178.150.111/newly.do?token=83c2eeed1c7bd454&code=bjpk10&format=json&rows=5";
    public static String pkUrl = "http://b.apiplus.net/newly.do?token=83c2eeed1c7bd454&code=bjpk10&format=json&rows=5";

    public static String charset = "UTF-8";

    public static void main(String[] args) {
        get(pkUrl, charset);
        //{"rows":5,"code":"bjpk10","remain":"730hrs","data":[{"expect":"591315","opencode":"06,03,09,02,08,05,04,07,01,10","opentime":"2016-12-10 16:42:48","opentimestamp":1481359368},{"expect":"591314","opencode":"06,03,07,04,10,09,05,01,02,08","opentime":"2016-12-10 16:37:51","opentimestamp":1481359071},{"expect":"591313","opencode":"08,09,06,02,03,05,07,04,01,10","opentime":"2016-12-10 16:32:52","opentimestamp":1481358772},{"expect":"591312","opencode":"08,01,10,02,09,07,03,05,04,06","opentime":"2016-12-10 16:27:44","opentimestamp":1481358464},{"expect":"591311","opencode":"04,10,01,05,03,08,06,07,09,02","opentime":"2016-12-10 16:22:36","opentimestamp":1481358156}]}
    }

    /**
     * @param urlAll  :请求接口
     * @param charset :字符编码
     * @return 返回json结果
     */
    public static String get(String urlAll, String charset) {
        BufferedReader reader = null;
        String result = null;
        String win = null;
        StringBuffer sbf = new StringBuffer();
        String userAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.66 Safari/537.36";// 模拟浏览器
        try {
            URL url = new URL(urlAll);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            connection.setReadTimeout(15000);
            connection.setConnectTimeout(15000);
            connection.setRequestProperty("User-agent", userAgent);
            connection.connect();
            InputStream is = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(is, charset));
            String strRead = null;
            while ((strRead = reader.readLine()) != null) {
                sbf.append(strRead);
                sbf.append("\r\n");
            }
            reader.close();
            result = sbf.toString();
            if (result == null) {
                return null;
            }
            JSONObject jsonObject = JSONObject.parseObject(result);
            JSONArray jsons = jsonObject.getJSONArray("data");
            int size = jsons.size();
            JSONObject obj = null;
            Map<String, String> map = null;
            Map<String, Object> mainMap = new HashMap<String, Object>();
            for (int i = 0; i < size; i++) {
                obj = (JSONObject) jsons.get(i);
                String expect = obj.getString("expect");
                String opencode = obj.getString("opencode");
                String opentime = obj.getString("opentime");
                map = new HashMap<String, String>();
                map.put("number", opencode);
                map.put("dateline", opentime);
                map.put("number", opencode);
                mainMap.put(expect, map);
            }
            win = JSON.toJSONString(mainMap);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return win;
    }
}